package com.cwj.mansysback.servlet.impl;

import com.cwj.mansysback.entity.Elective;
import com.cwj.mansysback.service.api.ElectiveService;
import com.cwj.mansysback.service.impl.ElectiveServiceImpl;
import com.cwj.mansysback.servlet.util.MyUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class GetElectiveServlet extends HttpServlet {

    private static final ElectiveService eleSer = new ElectiveServiceImpl();

    static class GetEleStatement extends Statement{
        private List<Elective> list =null;
        private Long count = 0L;

        public GetEleStatement(Integer statue, List<Elective> list) {
            super(statue);
            this.list = list;
        }

        public GetEleStatement(Integer statue, List<Elective> list,Long count) {
            super(statue);
            this.list = list;
            this.count = count;
        }

        public Long getCount() {
            return count;
        }

        public void setCount(Long count) {
            this.count = count;
        }

        public List<Elective> getList() {
            return list;
        }

        public void setList(List<Elective> list) {
            this.list = list;
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        if(req.getParameter("pageInd") ==null || req.getParameter("rowCount") ==null){
            MyUtils.writeResponse(resp,new Statement(8));
            return ;
        }

        int page = Integer.parseInt(req.getParameter("pageInd"));
        int rowCount = Integer.parseInt(req.getParameter("rowCount"));

        String sid = req.getParameter("sid");
        String cid = req.getParameter("cid");

        List<Elective> list = null;

        Long count =0L;

        if(req.getRequestURI().contains("/Lock/GetStudEle") && sid !=null){

            System.out.println(sid+"\t"+page+"\t"+rowCount);

            list = eleSer.selectSidElective(sid,page,rowCount);

            count = eleSer.selectPackBySid(sid).getCount();

        }else if(req.getRequestURI().contains("/Lock/GetCouEle") && cid != null){
            System.out.println("visit - /Lock/GetCouEle");
            list = eleSer.selectCidElective(cid,page,rowCount);

            count = eleSer.selectPackByCid(cid).getCount();

        }else if(req.getRequestURI().contains("/Lock/GetEle")){
            list = eleSer.selectElective(page,rowCount);
            count = eleSer.selectPack().getCount();
        }



        MyUtils.writeResponse(resp, new GetEleStatement(0, list,count));

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
